//
//  ASJF_Service.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/8/29.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "ASJF_Service.h"

static NSString *const UploadPicURL = ApplicationEnvironmentProduction ? @"http://fileserver.api.aishangjinrong.com/attachment" : @"http://fileserver.test.api.aishangjinrong.com/attachment";


@implementation ASJF_Service



/**
 省市区获取
 */
+(void)getAreaData {
    NSString *url = [Common ServicesURL:@"as/app/financial/location/provinceCityDistrict"];
    [[RequestManager manager] HFQDataRequest_Mao:RequestMethodGet URLString:url parameters:nil success:^(id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject;
        if ([[NSString stringWithFormat:@"%@",dataDic[@"code"]] isEqualToString:@"200"]){
            [AD_UserDefaults setObject:[dataDic objectForKey:@"data"] forKey:ASJF_Area];
            [AD_UserDefaults synchronize];
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
    }];
}



/**
 图片上传不可并发

 @param image 上传的图片
 @param success 返回URL
 @param failure 失败返回
 */
+ (void)uploadPictureWithData:(UIImage *)image Success:(nonnull void (^)(_Nullable id responseObject))success Failure:(nonnull void(^)(_Nullable id responseObject,  NSError * _Nullable error))failure {
    
    CGFloat compressionQuality;
    if(image.size.height > 3000 || image.size.width > 3000) {
        compressionQuality = 0.3;
    } else if (image.size.height > 1500 || image.size.width > 1500){
        compressionQuality = 0.5;
    } else {
        compressionQuality = 0.8;
    }
    NSData *data = UIImageJPEGRepresentation(image, compressionQuality);
    
    if (data) {
        
    }else {
        [HUD_Mao showCharacterMessage:@"图片解析出错，请重新拍照"];
        return;
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
    HFQFileObject *file = [[HFQFileObject alloc] init];
    file.name = @"file";
    file.fileName = fileName;
    file.mimeType = @"imagecc/png/jpeg/jpg";
    file.data = data;
    
    [ASJF_Service showHUDUploadCompleted];
    [[RequestManager manager] HFQUploadRequest_Mao:RequestMethodPost URLString:UploadPicURL parameters:nil file:file progress:^(int64_t totalUnitCount, int64_t completedUnitCount) {
        //进度
        [ASJF_Service showHUDUploadWithSpeed:1.0f *completedUnitCount / totalUnitCount];
    } success:^(id  _Nullable responseObject) {
        [ASJF_Service hideHUD];
        NSLog(@"%@",responseObject);
        
        
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            
//            data =     {
//                fileName = "20181011113218.jpg";
//                filePath = "http://data.aishangjinrong.com/PICTURES/2018/10/11/SMALL/998d2323cfb84522a8f0069016ad1ef2.jpg";
//                fileType = 2;
//                id = 1446701;
//            };
//            success = 1;
            
            success(responseObject);
        }else {
            failure(responseObject, nil);
            if (dic[@"massage"]) {
                [HUD_Mao showCharacterMessage:dic[@"massage"]];
            }else {
                [HUD_Mao showCharacterMessage:@"图片上传失败，请重新上传"];
            }
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        NSLog(@"%@, %@",responseObject,error);
        [ASJF_Service hideHUD];
        failure(responseObject, error);
        [HUD_Mao showCharacterMessage:@"图片上传失败，请重新上传"];
    }];
}


//ID_TYPE
//证件类型
//political
//政治面貌
//household
//户口性质
//marriStatus
//婚姻状况
//jobStatus
//在职状态
//nation
//民族
//education
//学历
//sex
//性别


/**
 枚举类获取
 
 @param enumeration 枚举的类型
 @param success 返回URL
 @param failure 失败返回
 */
+ (void)getEnumeration:(NSString *)enumeration Success:(nonnull void (^)(_Nullable id responseObject))success Failure:(nonnull void(^)(_Nullable id responseObject,  NSError * _Nullable error))failure {
    
    [[RequestManager manager] HFQDataRequest_Mao:RequestMethodPost URLString:@"" parameters:nil success:^(id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject;
        if ([[NSString stringWithFormat:@"%@",dataDic[@"code"]] isEqualToString:@"200"]) {
            
            [AD_UserDefaults setObject:@"" forKey:[NSString stringWithFormat:@"%@",enumeration]];
            [AD_UserDefaults synchronize];
            success(responseObject);
        }else {
            failure(responseObject, nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        failure(responseObject, error);
    }];
}


/**
 添加进度HUD
 */
+ (void)showHUDUploadCompleted {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow.rootViewController.view animated:YES];
    hud.mode = MBProgressHUDModeAnnularDeterminate;
    hud.label.text = @"图片上传中";
}


/**
 HUD进度展示
 
 @param speed 完成百分比
 */
+ (void)showHUDUploadWithSpeed:(CGFloat)speed {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD HUDForView:[UIApplication sharedApplication].keyWindow.rootViewController.view].progress = speed;
    });
}

+ (void)hideHUD {
    
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow.rootViewController.view animated:YES];
}

@end
